package com.saas.vertx.web.verticle;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.saas.util.EnumUtils;
import com.saas.vertx.common.ApiHandler;
import com.saas.vertx.common.RouterVerticle;
import io.vertx.core.AbstractVerticle;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: lzqing
 * @Description: 示例方法
 * @Date: Created in 2021/6/6
 */
@Component
public class CommonVerticle extends AbstractVerticle implements RouterVerticle {

    @Override
    public void start() throws Exception {
        super.start();
        routers.addAll(CollectionUtils.arrayToList(new ApiHandler[]{
                new ApiHandler(ApiHandler.all(), "/getOptionFromEnum", res -> {
                    Map map = JSONUtil.toBean(res.getBodyAsString(), Map.class);
                    String enumClassName = map.get("enumClassName") + "";
                    List<Map<String, String>> propsList = EnumUtils.getPropsList();
                    Optional<Map<String, String>> first = propsList.stream().filter(e -> {
                        return e.get(enumClassName) != null;
                    }).findFirst();
                    List<Map<String, Object>> list = new ArrayList<>();
                    if (first.isPresent()) {
                        Map<String, String> tmp = first.get();
                        String fullName = String.join("", "com.saas.enums.", tmp.get(enumClassName));
                        Class<?> aClass = null;
                        try {
                            aClass = Class.forName(fullName);
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                        list = EnumUtils.enumToListMap(aClass);
                    }
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.putOnce("data", list);
                    res.response().end(JSONUtil.toJsonStr(jsonObject));
                })}
        ));
    }
}
